package com.bw.publish

import android.Manifest
import android.os.Build
import android.os.Environment
import android.view.View
import androidx.annotation.RequiresApi
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.ToastUtils
import com.bw.lib_mvi.BaseActivity
import com.bw.publish.common.ConstPublish
import com.bw.publish.databinding.ActivityPublishBinding
import com.qiniu.pili.droid.shortvideo.PLAudioEncodeSetting
import com.qiniu.pili.droid.shortvideo.PLCameraSetting
import com.qiniu.pili.droid.shortvideo.PLFaceBeautySetting
import com.qiniu.pili.droid.shortvideo.PLMicrophoneSetting
import com.qiniu.pili.droid.shortvideo.PLRecordSetting
import com.qiniu.pili.droid.shortvideo.PLRecordStateListener
import com.qiniu.pili.droid.shortvideo.PLShortVideoRecorder
import com.qiniu.pili.droid.shortvideo.PLVideoEncodeSetting
import com.qiniu.pili.droid.shortvideo.PLVideoSaveListener
import java.io.File


@Route(path = ConstPublish.PUBLISH_TAKE)
class PublishActivity : BaseActivity(), PLRecordStateListener {
    lateinit var binding: ActivityPublishBinding
    val mShortVideoRecorder = PLShortVideoRecorder()
    var isRecorder: Boolean = true

    override fun getRootLayout(): View {
        binding = ActivityPublishBinding.inflate(layoutInflater)
        return binding.root
    }

    @RequiresApi(Build.VERSION_CODES.M)
    override fun initView() {
        super.initView()
        requestPermissions(arrayOf(Manifest.permission.CAMERA), 0)
        initQiNiu()
    }

    private fun initQiNiu() {

        mShortVideoRecorder.setRecordStateListener(this)
        // 摄像头采集选项
        val cameraSetting = PLCameraSetting()
        cameraSetting.setCameraId(PLCameraSetting.CAMERA_FACING_ID.CAMERA_FACING_FRONT)
        cameraSetting.setCameraPreviewSizeRatio(PLCameraSetting.CAMERA_PREVIEW_SIZE_RATIO.RATIO_4_3)
        cameraSetting.setCameraPreviewSizeLevel(PLCameraSetting.CAMERA_PREVIEW_SIZE_LEVEL.PREVIEW_SIZE_LEVEL_480P)

// 麦克风采集选项
        val microphoneSetting = PLMicrophoneSetting()

// 视频编码选项
        val videoEncodeSetting = PLVideoEncodeSetting(this)
        videoEncodeSetting.setEncodingSizeLevel(PLVideoEncodeSetting.VIDEO_ENCODING_SIZE_LEVEL.VIDEO_ENCODING_SIZE_LEVEL_480P_1) // 480x480
        videoEncodeSetting.setEncodingBitrate(1000 * 1024) // 1000kbps
        videoEncodeSetting.setEncodingFps(25)
        videoEncodeSetting.setHWCodecEnabled(true) // true:硬编 false:软编

// 音频编码选项
        val audioEncodeSetting = PLAudioEncodeSetting()
        audioEncodeSetting.setHWCodecEnabled(true) // true:硬编 false:软编

// 美颜选项
        val faceBeautySetting = PLFaceBeautySetting(1.0f, 0.5f, 0.5f)

// 录制选项
        val recordSetting = PLRecordSetting()
        recordSetting.setMaxRecordDuration((10 * 1000).toLong()) // 10s
        val path = Environment.getExternalStorageDirectory().absolutePath
        recordSetting.setVideoCacheDir(path)
        recordSetting.videoFilepath = path + File.separator + ConstPublish.VIDEO_RECORDER_PATH
// 设置录制速度 (默认为 1.0)
//        PLShortVideoRecorder.setRecordSpeed(1.0)

        mShortVideoRecorder.prepare(
            binding.glsPublish,
            cameraSetting,
            microphoneSetting,
            videoEncodeSetting,
            audioEncodeSetting,
            faceBeautySetting,
            recordSetting
        )
    }

    override fun initEvent() {
        super.initEvent()
        binding.tvBofang.setOnClickListener {
            if (isRecorder) {

                //开始一段视频
                mShortVideoRecorder.beginSection()
                ToastUtils.showLong("开始录制")
                binding.tvBofang.text = "结束"
            } else {

                //结束一段视频
                mShortVideoRecorder.endSection()
                //合成和保存所有的视频片段
                mShortVideoRecorder.concatSections(object : PLVideoSaveListener {
                    override fun onSaveVideoSuccess(p0: String?) {
                        ToastUtils.showLong("录制成功")
                    }

                    override fun onSaveVideoFailed(p0: Int) {
                        ToastUtils.showLong("录制失败")
                    }

                    override fun onSaveVideoCanceled() {

                    }

                    override fun onProgressUpdate(p0: Float) {

                    }
                })
                binding.tvBofang.text = "开始"

            }
            isRecorder = !isRecorder
        }
    }

    override fun onResume() {
        super.onResume()
        mShortVideoRecorder.resume()
    }

    override fun onPause() {
        super.onPause()
        mShortVideoRecorder.pause()
    }

    override fun onDestroy() {
        super.onDestroy()
        mShortVideoRecorder.destroy()
    }

    override fun onReady() {

    }

    override fun onError(p0: Int) {

    }

    override fun onDurationTooShort() {

    }

    override fun onRecordStarted() {

    }

    override fun onSectionRecording(p0: Long, p1: Long, p2: Int) {

    }

    override fun onRecordStopped() {

    }

    override fun onSectionIncreased(p0: Long, p1: Long, p2: Int) {

    }

    override fun onSectionDecreased(p0: Long, p1: Long, p2: Int) {

    }

    override fun onRecordCompleted() {

    }
}

